package com.hghivln.blog.controller;


import com.hghivln.blog.common.lang.Result;
import com.hghivln.blog.entity.SysUser;
import com.hghivln.blog.entity.Type;
import com.hghivln.blog.service.TypeService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 帖子类型表 前端控制器
 * </p>
 *
 * @author hghivln
 * @since 2021-09-03
 */
@RestController
@RequestMapping("/type")
public class TypeController {

    @Autowired
    private TypeService typeService;

    /**
     * 保存
     */
    @PostMapping("save")
    public Result<?> addType(@RequestBody Type type) {
        try {
            typeService.save(type);
            return new Result<>("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>("保存失败");
        }
    }

    /**
     * 更新
     */
    @PutMapping("update")
    public Result<?> update(@RequestBody Type type) {
        try {
            typeService.updateById(type);
            return new Result<>("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>("修改失败");
        }
    }

    /**
     * 删除
     */
    @DeleteMapping( "/delete/{id}")
    public Result<?> deleteById(@PathVariable Long id) {
        try {
            typeService.deleteById(id);
            return new Result<>("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>("删除失败");
        }
    }

    /**
     * 根据ID查询
     */
    @GetMapping("/get/{id}")
    public Result<Type> get(@PathVariable Long id) {
        Type type = typeService.getById(id);
        return new Result<>(type);
    }

    /**
     * 列表查询
     */
    @GetMapping("list")
    public Result<List<Type>> get() {
        List<Type> list = typeService.getAllList();
        return new Result<>(list);
    }
}
